package com.imageim.yimirror.ui.widght;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.wang.avi.AVLoadingIndicatorView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Yuaihen.
 * on 2018/11/30
 * 加载进度条View
 */
public class LoadingView extends LinearLayout {

    @BindView(R.id.av_loading)
    AVLoadingIndicatorView mAvLoading;
    @BindView(R.id.tv_loading_text)
    TextView mTvLoadingText;
    private Unbinder mBind;

    public static final String BallBeatIndicator = "BallBeatIndicator";
    public static final String BallClipRotateIndicator = "BallClipRotateIndicator";
    public static final String BallClipRotateMultipleIndicator = "BallClipRotateMultipleIndicator";
    public static final String BallClipRotatePulseIndicator = "BallClipRotatePulseIndicator";
    public static final String BallGridBeatIndicator = "BallGridBeatIndicator";
    public static final String BallGridPulseIndicator = "BallGridPulseIndicator";
    public static final String BallPulseIndicator = "BallPulseIndicator";
    public static final String BallPulseRiseIndicator = "BallPulseRiseIndicator";
    public static final String BallPulseSyncIndicator = "BallPulseSyncIndicator";
    public static final String BallRotateIndicator = "BallRotateIndicator";
    public static final String BallScaleIndicator = "BallScaleIndicator";
    public static final String BallScaleRippleIndicator = "BallScaleRippleIndicator";
    public static final String BallScaleMultipleIndicator = "BallScaleMultipleIndicator";
    public static final String BallScaleRippleMultipleIndicator = "BallScaleRippleMultipleIndicator";
    public static final String BallSpinFadeLoaderIndicator = "BallSpinFadeLoaderIndicator";
    public static final String BallTrianglePathIndicator = "BallTrianglePathIndicator";
    public static final String BallZigZagDeflectIndicator = "BallZigZagDeflectIndicator";
    public static final String BallZigZagIndicator = "BallZigZagIndicator";
    public static final String LineScaleIndicator = "LineScaleIndicator";
    public static final String LineScalePartyIndicator = "LineScalePartyIndicator";
    public static final String LineScalePulseOutIndicator = "LineScalePulseOutIndicator";
    public static final String LineScalePulseOutRapidIndicator = "LineScalePulseOutRapidIndicator";
    public static final String LineSpinFadeLoaderIndicator = "LineSpinFadeLoaderIndicator";
    public static final String PacmanIndicator = "PacmanIndicator";
    public static final String SemiCircleSpinIndicator = "SemiCircleSpinIndicator";
    public static final String SquareSpinIndicator = "SquareSpinIndicator";
    public static final String TriangleSkewSpinIndicator = "TriangleSkewSpinIndicator";
    private View mView;

    public LoadingView(Context context) {
        super(context);
        init(context);
    }


    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.view_loading, this);
        mBind = ButterKnife.bind(this, mView);
    }


    /**
     * 设置加载的文字
     */
    public void setLoadingText(String loadingText) {
        mTvLoadingText.setText(loadingText);
    }

    /**
     * 设置指示器样式
     */
    public void setIndicatorsStyle(String loadingStyle) {
        mAvLoading.setIndicator(loadingStyle);
    }

    /**
     * 设置指示器的颜色
     *
     * @param rgbColor RGB十六进制数值 #FFFFFF
     */
    public void setIndicatorsColor(String rgbColor) {
        if (!TextUtils.isEmpty(rgbColor) && rgbColor.startsWith("#")) {
            int color = Color.parseColor(rgbColor);
            mAvLoading.setIndicatorColor(color);
        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mBind != null) {
            mBind.unbind();
        }
    }

    public void hide() {
        mView.setVisibility(GONE);
    }

    public void show() {
        mView.setVisibility(VISIBLE);
    }
}
